package com.bluesky.objectworld;

import com.bluesky.objectworld.primitive.PrimitiveType;
import com.bluesky.objectworld.primitive._Boolean;
import com.bluesky.objectworld.primitive._Integer;
import com.bluesky.objectworld.primitive._String;

public class World extends ObjectImpl {

	long objectId = 0;

	public World() {

	}

	private long getNextObjectId() {
		objectId++;
		return objectId;
	}

	/**
	 * a convenient way for native method to create primitive object
	 * @param type
	 * @param literalValue
	 * @return
	 */
	public _Object create(PrimitiveType type, String literalValue) {
		ObjectImpl result = null;
		if (type == PrimitiveType.Integer) {
			_Integer obj = new _Integer();
			obj.value = Integer.valueOf(literalValue);

			result = obj;
		} else if (type == PrimitiveType.String) {
			_String obj = new _String();
			obj.value = String.valueOf(literalValue);

			result = obj;
		} else if (type == PrimitiveType.Boolean) {
			_Boolean obj = new _Boolean();
			obj.value = Boolean.valueOf(literalValue);

			result = obj;
		}
		
		result.id = getNextObjectId();
		
		
		return result;
	}

	@Override
	public _Object processMessage(String name, _Object[] parameters) {

		/**
		 * layEgg()
		 */
		if (name.equals("layEgg")) {

			ObjectImpl obj = null;
			int len = parameters.length;
			switch (len) {
			case 0:
				obj = new ObjectImpl();

				break;
			case 1:// p1: any Java Class that support AwObject interface.
				_String javaClassName = (_String) parameters[0];

				try {
					obj = (ObjectImpl) getClass().forName(javaClassName.value)
							.newInstance();
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
				break;
			}
			obj.id = getNextObjectId();
			return obj;

		} 
		
		return null;
	}

}
